-
Notifications
You must be signed in to change notification settings - Fork 0
test: add api tests #103
test: add api tests #103
Conversation
|
One decision I'd like some input on is the database the API is tested against. This implementation creates a small local test database for each run, and never touches the production database. I couldn't find any guidelines in the project description on exactly what to use, so I'll assume this is fine. I think the E2E tests should run against a local setup as well, rather than the production database on the VM. Otherwise they would create test users and favorites in the real data, and depend on data that can change. We can probably reuse this test database setup for that. |
I think this is a good choice, and it sounds like you have some good ideas for some of the other tests as well |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice to have the API covered by tests! All tests pass
|
Should we have a separate issue to get these tests to run in CI, or would you like to do this here? |
|
Separate. I can fix that. |
Summary
Adds tests for our GraphQL API. The tests send GraphQL queries through our schema and resolvers to a real Postgres database and check the responses, using Apollo Server's
executeOperation(no HTTP server). The logged-in user is set directly in the context, since that's all the resolvers read from the session.How it works
pnpm testinbackendruns its own Postgres, separate from the development database:test-db-data(gitignored) and starts Postgres from it on port 5434, so it doesn't collide with the development database on 5433.readr_test, runs our migrations and inserts 7 test books and 2 test users fromtests/fixtures.ts.You only need PostgreSQL installed, not
pnpm db:setupor the book dataset. As a safety check, the setup refuses to reset a database whose name doesn't end with_test.What is tested
totalCountcounting all matches, sorting by rating across all matches (ties by number of ratings, unrated books last),book(id)with genres andnullfor a missing book, and the filter option lists.To check that the tests catch real bugs, I broke the sorting, counting and title search in
searchBooksone at a time before mergingmain. The right test failed each time.Other changes
backend/README.md: new "Testing the API" section.docs/infrastructure.md: Vitest added under backend.frontend/README.md: the test section was outdated after test: rewrite tests with Apollo MockedProvider #85 and is updated.mainand updated the tests to the current API:queryinstead oftitle, andRELEVANCE(most ratings first) as the default sort.How to test
AI usage
The tests and this description were generated with AI (Claude) and reviewed before opening the PR.
Closes #102